'''
Created on 2009-12-14

@author: yuchen
'''

'''
Created on 2009-11-18

@author: yuchen
'''

import ai.abstract_player
import random

class AIcomputer(ai.abstract_player.AIPlayer):
	
	def __init__(self, player):
		ai.abstract_player.AIPlayer.__init__(self, player)
		self.tempoPiece = [None, None, None, None]
		self.player = [None, None]
		self.tempoState = 1
		self.drawCondition1 = [0, 0, 0, 0]
		self.drawCondition2 = 0
		self.drawCondition3 = [0, 0]
		
	def get_move(self, state):
		# get all moves
		moves = state.successorMove()
		player = state.getCurrentPlayer()			 
		if moves == None :						 
			return None
		elif len(moves) == 1:
			return moves[0]
		
		if self.get_draw(state) == True :
			return 'draw'
		
		move = random.choice(moves)	
				
		return move

	def get_draw(self, state):
		moves = state.successorMove()
		player = state.getCurrentPlayer()
		if self.player[0] == None :
			self.player[0] = state.board.getPlayer0()
			self.player[1] = state.board.getPlayer1()
			number = 0
		else :
			if self.player[0] == player :
				number = 0
			else :
				number = 1
				
		# judge whether a state has been repeated 3 times
		self.tempoState = 1 - self.tempoState
		tempPiece0 = []
		tempPiece1 = []
		for piece in self.player[number].pieces :
			tempPiece0.append(piece.position)
		tempPiece0.sort()
		
		for piece in self.player[1 - number].pieces :
			tempPiece1.append(piece.position)
		tempPiece1.sort()
		
		if self.tempoPiece[number + 2 * self.tempoState] == None :
			self.tempoPiece[number + 2 * self.tempoState] = tempPiece0
			self.tempoPiece[1 - number + 2 * self.tempoState] = tempPiece1
			self.drawCondition1[number + 2 * self.tempoState] += 1
		else :
			if self.tempoPiece[number + 2 * self.tempoState] == tempPiece0 and\
			self.tempoPiece[1 - number + 2 * self.tempoState] == tempPiece1 :
				self.drawCondition1[number + 2 * self.tempoState] += 1
			else :
				self.tempoPiece[number + 2 * self.tempoState] = tempPiece0
				self.tempoPiece[1 - number + 2 * self.tempoState] = tempPiece1
				self.drawCondition1[number + 2 * self.tempoState] = 1
		
		if self.drawCondition1[number + 2 * self.tempoState] == 3 :
			return True
		
		# only move king and do not eat any piece in 25 turns
		currentPiece = state.board.getBoardPosition(moves[number].list[0].original[0], moves[number].list[0].original[1], player)
		if currentPiece.king == True and\
		abs(moves[number].list[0].original[0] - moves[number].list[0].destination[0]) == 1 and\
		abs(moves[number].list[0].original[1] - moves[number].list[0].destination[1]) == 1 :
			self.drawCondition2 += 1
		else :
			self.drawCondition2 = 0
		
		if self.drawCondition2 == 50 :
			return True
		
		# if one player have less than 3 pieces, and the other one player has only one king in 16 turns.
		if len(tempPiece0) <= 3 and len(tempPiece1) == 1 :
			if self.player[1 - number].pieces.king == True :
				self.drawCondition3[number] += 1
		
		if self.drawCondition3[number] == 16 :
			return True
		
		return False